﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Vtron.LEDCorrective.Core;
using Vtron.LEDCorrective.Pages;

namespace Vtron.LEDCorrective
{
    /// <summary>
    /// TopologicalWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TopologicalWindow : Window
    {
        public TopologicalWindow()
            : this(null)
        {
        }

        private PartitionCorrective partitionCorrective = null;
        public TopologicalWindow(PartitionCorrective partitionCorrective)
        {
            InitializeComponent();

            Loaded += TopologicalWindow_Loaded;

            this.partitionCorrective = partitionCorrective;
        }

        private void TopologicalWindow_Loaded(object sender, RoutedEventArgs e)
        {
            CorrectiveEngine correctiveEngine = CorrectiveEngine.Instance;
            double gridWith = content_Grid.ActualWidth;
            double gridHight = content_Grid.ActualHeight;
            double scaleX = gridWith / correctiveEngine.WallWidth;
            double scaleY = gridHight / correctiveEngine.WallHeight;
            double scale = scaleX > scaleY ? scaleY : scaleX;

            Grid grid = new Grid();
            grid.ShowGridLines = true;

            for (int i = 0; i < correctiveEngine.SeparateColumn; i++)
            {
                ColumnDefinition columnDefinition = new ColumnDefinition();
                double columnWidth = (i + 1) * correctiveEngine.PartitionColumn > correctiveEngine.WallWidth ?
                    ((i + 1) * correctiveEngine.PartitionColumn - correctiveEngine.WallWidth) * scale :
                    correctiveEngine.PartitionColumn * scale;
                columnDefinition.Width = new GridLength(columnWidth);

                grid.ColumnDefinitions.Add(columnDefinition);
            }

            for (int i = 0; i < correctiveEngine.SeparateRow; i++)
            {
                RowDefinition rowDefinition = new RowDefinition();
                double rowHeight = (i + 1) * correctiveEngine.PartitionRow > correctiveEngine.WallHeight ?
                    ((i + 1) * correctiveEngine.PartitionRow - correctiveEngine.WallHeight) * scale :
                    correctiveEngine.PartitionRow * scale;
                rowDefinition.Height = new GridLength(rowHeight);

                grid.RowDefinitions.Add(rowDefinition);
            }

            for (int i = 0; i < correctiveEngine.SeparateColumn * correctiveEngine.SeparateRow; i++)
            {
                Label label = new Label();
                label.Content = String.Format("{0}", i + 1);
                label.SetValue(Grid.ColumnProperty, i % correctiveEngine.SeparateColumn);
                label.SetValue(Grid.RowProperty, (int)Math.Floor((double)i / correctiveEngine.SeparateColumn));
                if (correctiveEngine.LedCollections[i].IsDone)
                {
                    label.Background = new SolidColorBrush(Colors.Green);
                }
                else
                {
                    label.Background = new SolidColorBrush(Colors.Beige);
                }
                label.Tag = i + 1;
                label.MouseLeftButtonUp += label_MouseLeftButtonUp;

                grid.Children.Add(label);
            }

            Canvas canvas = new Canvas();
            canvas.Children.Add(grid);
            canvas.HorizontalAlignment = HorizontalAlignment.Left;
            canvas.VerticalAlignment = VerticalAlignment.Top;
            double l = (content_Grid.ActualWidth - correctiveEngine.WallWidth * scale) / 2;
            double t = (content_Grid.ActualHeight - correctiveEngine.WallHeight * scale) / 2;
            canvas.Margin = new Thickness(l, t, 0, 0);
            content_Grid.Children.Add(canvas);
        }

        private void label_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Label label = sender as Label;

            if (partitionCorrective == null)
            {
                ImageWindow imageWindow = new ImageWindow((int)label.Tag);
                imageWindow.Show();
            }
            else
            {
                partitionCorrective.PartitionNum = (int)label.Tag;
                Close();
            }
        }
    }
}
